import pymysql
class DBTools(object):
    def __init__(self):
        self.conn = pymysql.connect(host="211.103.136.244", port=7061,
                               user="student", password="iHRM_student_2021",
                               database="ihrm", charset="utf8")
        self.cs = self.conn.cursor()
    def __del__(self):
        self.cs.close()
        self.conn.close()

    def query_one(self,sql):
        """查询方法"""
        try:
            self.cs.execute(sql)
            return self.cs.fetchone()
        except Exception as e:
            print("只需要打印", e)

    def db_uid(self,sql):
        """增删改方法"""
        try:
            self.cs = self.conn.cursor()
            res = self.cs.execute(sql)
            print("影响的行数", res)
            self.conn.commit()

        except Exception as e:
            print("打印错误结果", e)
            self.conn.rollback()


if __name__ == '__main__':
    db=DBTools()
    print(db.query_one("select * from t_book;"))
    print(db.query_one("select * from t_hero;"))
    db.db_uid("insert into t_book(title, pub_date) values('好好学习吧', '2022-01-02');")
    db.db_uid("delete from t_book where title='捉妖记';")
    db.db_uid("update t_book set title='好好学接口' where pub_date='2022-01-02';")
    db.db_uid("delete from t_book where title='捉妖记';")
    db.db_uid("UPDATE t_book SET `read` = `read` + 1 WHERE id = 1;")


